﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Audio;
namespace GlobalGameJam
{
    class Snake
    {
        public struct Point
        {
            public int X;
            public int Y;
        }

        public List<Point> Body;
        public int direction;
        public Vector2 max;
        public Point newPoint(int x, int y)
        {
            Point p = new Point();
            p.X = x;
            p.Y = y;
            return p;
        }

        public Snake(int x, int y)
        {
            this.Body = new List<Point>();
            this.Body.Add(this.newPoint(x, y));

            this.Body.Add(this.newPoint(x, y + 1));
            this.Body.Add(this.newPoint(x, y + 2));
            this.direction = 0;
        }

        public void Initialize(Vector2 max)
        {
            this.max = max;
        }


        public bool isOnPoint(Point P)
        {
            return this.Body.Contains(P);
        }

        public bool eatMyself()
        {
            for (int i = 1; i < this.Body.Count; i++)
                if (this.Body[0].X == this.Body[i].X && this.Body[0].Y == this.Body[i].Y)
                    return true;
            return false;
        }

        public void Update()
        {
            Point p = this.Body[0];
            switch (this.direction)
            {
                case 0:
                    if (p.Y + 1 > this.max.Y)
                        this.Body[0] = this.newPoint(p.X, 0);
                    else
                        this.Body[0] = this.newPoint(p.X, p.Y + 1);
                    break;
                case 1:
                    if (p.Y - 1 < 0)
                        this.Body[0] = this.newPoint(p.X, (int)max.Y);
                    else
                        this.Body[0] = this.newPoint(p.X, p.Y - 1);
                    break;
                case 2:
                    if (p.X + 1 > this.max.X)
                        this.Body[0] = this.newPoint(0, p.Y);
                    else
                        this.Body[0] = this.newPoint(p.X + 1, p.Y);
                    break;
                case 3:
                    if (p.X - 1 < 0)
                        this.Body[0] = this.newPoint((int) this.max.X, p.Y);
                    else
                        this.Body[0] = this.newPoint(p.X - 1, p.Y);
                    break;
                   
            }
            for (int i = 1; i < this.Body.Count; i++)
            {
                Point p1 = this.Body[i];
                this.Body[i] = p;
                p = p1;
            }
        }


        public void Draw()
        {
            Console.WriteLine("start snake");
            for (int i = 0; i < this.Body.Count; i++)
                Console.WriteLine("body : " + this.Body[i].X + " " + this.Body[i].Y);
            Console.WriteLine("end snake");
        }
    }
}
